//
//  AppDelegate.m
//  UI02_UILable_UITextField_UIButton_UIImageView
//
//  Created by wyx on 16/3/10.
//  Copyright © 2016年 Apon. All rights reserved.
//

#import "AppDelegate.h"
#import "ViewController.h"
#import "MyLable.h"
#import "TextFields.h"
#import "MyButton.h"
#import "MyImageView.h"
@interface AppDelegate () <UITextFieldDelegate>//协议
@end
@implementation AppDelegate
/** 重写 dealloc */
-(void)dealloc{
    [_window release];
    [super dealloc];
}
#pragma mark - 主视图
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.
    self.window = [[UIWindow alloc] initWithFrame:[UIScreen mainScreen].bounds];
    self.window.backgroundColor = [UIColor cyanColor];
    [self.window makeKeyAndVisible];
    ViewController *vc = [[ViewController alloc] init];
    self.window.rootViewController = vc;
    [_window release];
    [vc release];
    //UIButton
    [self createLable];
    //UITextField
    [self createTextField];
    //UIButton
    [self createButton];
    //UIImageView
    [self creatImageView];
    return YES;
}
#pragma mark - 知识点1:UILable
- (void)createLable{
    MyLable *lableOfUserName = [[MyLable alloc] initWithFrame:CGRectMake(20, 50, 100, 40) text:@"用户名:"];
    [self.window addSubview:lableOfUserName];
    [lableOfUserName release];
    
    MyLable *lableOfKey = [[MyLable alloc] initWithFrame:CGRectMake(20, 120, 100, 40) text:@"密码:"];
    [self.window addSubview:lableOfKey];
    [lableOfKey release];
}
#pragma mark - 知识点2:UITextField
- (void)createTextField{
    TextFields *textFieldOfUserName = [[TextFields alloc] initWithFrame:CGRectMake(150, 50, 200, 40) placeHolder:@"请输入用户名"];
    [self.window addSubview:textFieldOfUserName];
    [textFieldOfUserName release];
    textFieldOfUserName.tag = 100;
    textFieldOfUserName.delegate = self;//指定代理人 self 此时指 appdelegate 对象本身
    TextFields *textFiledOfKey = [[TextFields alloc] initWithFrame:CGRectMake(150, 120, 200, 40) placeHolder:@"请输入密码"];
    [self.window addSubview:textFiledOfKey];
    [textFiledOfKey release];
    textFiledOfKey.tag = 200;
    textFiledOfKey.delegate = self;
    //密文输入
    textFiledOfKey.secureTextEntry = YES;
    //键盘
    textFiledOfKey.keyboardType = UIKeyboardTypeDefault;
    //回车键 style
    textFiledOfKey.returnKeyType = UIReturnKeyJoin;
    
    
}
#pragma mark - 知识点3:UITextField 协议方法
/** 点击键盘上的 enter 键 */
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    //隐藏键盘
    switch (textField.tag) {
        case 200:
            [textField resignFirstResponder];
            break;
        default:
            break;
    }
    return YES;
}
/** 文本内容发生变化 */
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    //打印位置长度
    //NSLog(@"%ld %ld",range.location, range.length);
    //限制输入数字
    if ([string isEqualToString:@"1"]) {
        
        return NO;
    }
    //限制输入6个字符
    else if (range.location > 5){
        return NO;
    }
    else{
        return YES;
    }
}
/** 文本将要开始编辑 用户名为空不可以键入密码 调用此方法 */
- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField {
    //获取对象
    TextFields *user = [self.window viewWithTag:100];
    if ( textField.tag == 200 && [user.text isEqualToString:@""]) {
        return NO;
    }
    return YES;
    
}
#pragma mark - 知识点4: UIButton
- (void)createButton{
    //创建对象确认键
    MyButton *buttonOfOk = [MyButton buttonWithType:UIButtonTypeSystem];
    [self.window addSubview:buttonOfOk];
    buttonOfOk.frame = CGRectMake(70, 200, 120, 40);
    //buttonOfOk.backgroundColor = [UIColor yellowColor];
    [buttonOfOk setTitle:@"确认" forState: UIControlStateNormal];//标题
    buttonOfOk.titleLabel.font = [UIFont systemFontOfSize:23];//字体
    [buttonOfOk addTarget:self action:@selector(handleOk:) forControlEvents:UIControlEventTouchUpInside];
    [buttonOfOk setTag:100];
    [buttonOfOk setBackgroundImage:[UIImage imageNamed:@"fb607ccec502ce6b3a385863de6204d1"] forState:UIControlStateNormal];
    //创建对象取消键
    MyButton *buttonOfCancle = [MyButton buttonWithType:UIButtonTypeCustom];
    [self.window addSubview:buttonOfCancle];
    buttonOfCancle.frame = CGRectMake(230, 200, 100, 40);
    //buttonOfCancle.backgroundColor = [UIColor yellowColor];
    buttonOfCancle.titleLabel.font = [UIFont systemFontOfSize:23];
    //设置背景图片
    //[buttonOfCancle setBackgroundImage:[UIImage imageNamed:@"428a7d7b5773d54bf7dea977bab668ec"] forState:UIControlStateNormal];
    //设置前景图片
    [buttonOfCancle setImage:[UIImage imageNamed:@"428a7d7b5773d54bf7dea977bab668ec"] forState:UIControlStateNormal];
    [buttonOfCancle setTitle:@"取消" forState: UIControlStateNormal];
    
}
- (void)handleOk:(UIButton *)button{
    //回收键盘
    [self.window endEditing:YES];
}
#pragma mark - 知识点5: UIImageview
- (void)creatImageView {
    //创建对象
    MyImageView *imageView = [[MyImageView alloc] initWithImage:[UIImage imageNamed:@"9ac886ba73f49e0d9ee22ec6f941054b"]];
    imageView.frame = CGRectMake(100, 300, 200, 200);
    [self.window addSubview:imageView];
    [imageView release];
}
#pragma mark - 其他协议方法
- (void)applicationWillResignActive:(UIApplication *)application {
    // Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
    // Use this method to pause ongoing tasks, disable timers, and throttle down OpenGL ES frame rates. Games should use this method to pause the game.
}

- (void)applicationDidEnterBackground:(UIApplication *)application {
    // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
    // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
}

- (void)applicationWillEnterForeground:(UIApplication *)application {
    // Called as part of the transition from the background to the inactive state; here you can undo many of the changes made on entering the background.
}

- (void)applicationDidBecomeActive:(UIApplication *)application {
    // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
}

- (void)applicationWillTerminate:(UIApplication *)application {
    // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
}

@end
